﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using web.data.entities;
using System.Configuration;
using Webdiyer.WebControls.Mvc;
using System.Web.UI;
using System.Linq.Expressions;
using System.Reflection;
using System.Xml.Schema;

namespace web.bussines
{
    public class article_service
    {
        #region v_category_article
        public v_category_article GetViewArticleCategoryByID(int id, int? languageId)
        {
            using (var data = new baseEntities())
            {
                if (id != 0)
                {
                    return data.view_category_article.Where(p => p.category_id == id && p.language_id == languageId).FirstOrDefault();
                }
                else
                {
                    return new v_category_article();
                }

            }
        }

        /// <summary>
        /// Get List category for adm page 
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        public List<v_category_article> GetListCategoryArticleByParentAdm(int? cateId, int? languageId)
        {
            using (var data = new baseEntities())
            {
                languageId = languageId ?? 1;   //default language 1 (vi) if null language
                List<v_category_article> list_category = null;

                if (cateId == null)
                {
                    list_category = (from p in data.view_category_article
                                     where p.category_parent == null && p.language_id == languageId
                                     select p).ToList();
                }
                else
                {
                    list_category = (from p in data.view_category_article
                                     where p.category_parent == cateId && p.language_id == languageId
                                     select p).ToList();
                }

                return list_category;
            }
        }

        /// <summary>
        /// Get List category for public page 
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        public List<v_category_article> GetListCategoryArticleByParent(int? cateId, int? languageId)
        {
            using (var data = new baseEntities())
            {
                languageId = languageId ?? 1;   //default language 1 (vi) if null language
                List<v_category_article> list_category = null;

                if (cateId == null)
                {
                    list_category = (from p in data.view_category_article
                                     where p.category_parent == null && p.category_status == 1 && p.language_id == languageId
                                     select p).ToList();
                }
                else
                {
                    list_category = (from p in data.view_category_article
                                     where p.category_parent == cateId && p.category_status == 1 && p.language_id == languageId
                                     select p).ToList();
                }

                return list_category;
            }
        }

        /// <summary>
        /// for adm page
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pageSize"></param>
        /// <param name="text_search"></param>
        /// <param name="parent"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public List<v_category_article> getCategoryArticleBuilder(string text_search, int? parent, int? languageId)
        {
            using (var data = new baseEntities())
            {
                List<v_category_article> list = getListCategoryArticleBuilder(parent, languageId);

                if (!string.IsNullOrEmpty(text_search))
                {
                    list = list.Where(p => (p.category_name != null ? p.category_name.Contains(text_search) : string.Empty.Contains(text_search)) ||
                        (p.category_desc_short != null ? p.category_desc_short.Contains(text_search) : string.Empty.Contains(text_search)) ||
                        (p.category_desc != null ? p.category_desc.Contains(text_search) : string.Empty.Contains(text_search))).ToList();
                }

                return list.ToList();
            }
        }
        /// <summary>
        /// for adm page
        /// </summary>
        /// <param name="id"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public List<v_category_article> getListCategoryArticleBuilder(int? id, int? languageId)
        {
            List<v_category_article> sb = new List<v_category_article>();
            return getListCategoryArticleBuilder(ref sb, id, languageId, true);
        }
        /// <summary>
        /// for adm page
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="id"></param>
        /// <param name="languageId"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        public List<v_category_article> getListCategoryArticleBuilder(ref List<v_category_article> sb, int? id, int? languageId, bool root)
        {
            List<v_category_article> list_parent = GetListCategoryArticleByParentAdm(id, languageId);
            foreach (var item in list_parent)
            {
                sb.Add(item);
                //get list child of item
                List<v_category_article> list_child = GetListCategoryArticleByParentAdm(item.category_id, languageId);
                if (list_child.Count() > 0)
                {
                    getListCategoryArticleBuilder(ref sb, item.category_id, languageId, false);
                }

            }
            return sb;
        }

        /// <summary>
        /// for drop downlist
        /// </summary>
        /// <param name="id"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public List<v_category_article> getListCategoryArticleDropDownlist(int? id, int? languageId)
        {
            List<v_category_article> sb = new List<v_category_article>();
            return getListCategoryArticleDropDownlist(ref sb, id, languageId, true);
        }
        /// <summary>
        /// for adm page
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="id"></param>
        /// <param name="languageId"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        public List<v_category_article> getListCategoryArticleDropDownlist(ref List<v_category_article> sb, int? id, int? languageId, bool root)
        {
            List<v_category_article> list_parent = GetListCategoryArticleByParentAdm(id, languageId);
            foreach (var item in list_parent)
            {
                if (!root)
                {
                    item.category_name = "--" + item.category_name;
                    v_category_article pa = GetViewArticleCategoryByID(id ?? 0, languageId);
                    foreach (var itemStr in sb)
                    {
                        if (pa != null)
                        {
                            if (itemStr.category_id == pa.category_id)
                            {
                                if (itemStr.category_name.Contains("--"))
                                {
                                    int laststr = itemStr.category_name.LastIndexOf("--") + 2;
                                    string addStr = itemStr.category_name.Substring(0, laststr);
                                    item.category_name = addStr + item.category_name;
                                }
                            }
                        }
                    }
                }
                sb.Add(item);
                //get list child of item
                List<v_category_article> list_child = GetListCategoryArticleByParentAdm(item.category_id, languageId);
                if (list_child.Count() > 0)
                {

                    getListCategoryArticleDropDownlist(ref sb, item.category_id, languageId, false);
                }

            }
            return sb;
        }

        #endregion

        #region article_category
        //update status
        public void updateStatusArticleCategory(article_category cate)
        {
            using (var data = new baseEntities())
            {
                var cate_change_status = data.tbl_article_category.Where(p => p.category_id == cate.category_id).SingleOrDefault();

                cate_change_status.category_status = cate.category_status;
                data.SaveChanges();
            }
        }
        //create
        public int createArticleCategory(article_category cate, List<article_category_item> items)
        {
            using (var data = new baseEntities())
            {
                foreach (var item in items)
                {
                    cate.tbl_article_category_item.Add(item);
                }
                data.AddTotbl_article_category(cate);
                data.SaveChanges();

                return cate.category_id;
            }
        }

        /// <summary>
        /// Đệ quy list ID danh mục bai viet adm page
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public StringBuilder GetListCategoryArticleIdBuilder(ref StringBuilder sb, int cateId)
        {
            using (var data = new baseEntities())
            {
                var cate = (from p in data.view_category_article
                            where p.category_parent == cateId
                            select p.category_id).ToList();
                foreach (var item in cate)
                {
                    sb.Append(item + ",");
                    if (cate.Count() > 0)
                    {
                        GetListCategoryArticleIdBuilder(ref sb, item);
                    }
                }

                return sb;
            }
        }

        //delete
        public void deleteArticleCategory(int category_id)
        {
            using (var data = new baseEntities())
            {
                var em = data.tbl_article_category.Where(p => p.category_id == category_id).FirstOrDefault();

                if (em != null)
                {


                    //Xoa all category is child
                    StringBuilder sb = new StringBuilder();
                    sb = GetListCategoryArticleIdBuilder(ref sb, (int)em.category_id);
                    if (sb.ToString().EndsWith(","))
                    {
                        var arrId = sb.ToString().Substring(0, sb.Length - 1).Split(',');
                        int[] arrIdInt = arrId.Select(n => Convert.ToInt32(n)).ToArray();
                        var list_child_category = data.tbl_article_category.Where(p => arrIdInt.Contains(p.category_id)).ToList();
                        var list_child_category_id = data.tbl_article_category.Where(p => arrIdInt.Contains(p.category_id)).ToList().Select(m => m.category_id).ToArray();

                        //Xoa article_category_item of list child
                        var list_item_child = data.tbl_article_category_item.Where(p => list_child_category_id.Contains((int)p.category_id)).ToList();
                        foreach (var it in list_item_child)
                        {
                            data.DeleteObject(it);
                        }

                        //xoa article_category child in ref article
                        var list_child_ref = data.tbl_article_ref_category.Where(p => list_child_category_id.Contains((int)p.category_id)).ToList();
                        foreach (var it in list_child_ref)
                        {
                            data.DeleteObject(it);
                        }

                        //xoa list child
                        foreach (var it in list_child_category)
                        {
                            data.DeleteObject(it);
                        }
                    }
                    else
                    {

                    }

                    //Xoa article_category_item of category
                    var list_item = data.tbl_article_category_item.Where(p => p.category_id == em.category_id).ToList();
                    foreach (var it in list_item)
                    {
                        data.DeleteObject(it);
                    }

                    //Xoa article_category in ref article
                    var list_ref = data.tbl_article_ref_category.Where(p => p.category_id == em.category_id).ToList();
                    foreach (var it in list_ref)
                    {
                        data.DeleteObject(it);
                    }

                    //Xoa employee object
                    data.DeleteObject(em);
                    data.SaveChanges();
                }


            }
        }

        /// <summary>
        /// get list Article Category Items for cateId
        /// </summary>
        /// <param name="category_id"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public List<web.data.entities.article_category_item> GetListArticleCategoryItemsByCateID(int category_id, int? languageId)
        {
            using (var data = new baseEntities())
            {
                List<web.data.entities.article_category_item> list = new List<article_category_item>();
                if (languageId != null)
                {
                    list = data.tbl_article_category_item.Where(p => p.category_id == category_id && p.language_id == languageId).ToList();
                }
                else
                {
                    list = data.tbl_article_category_item.Where(p => p.category_id == category_id).ToList();
                }

                return list;
            }
        }

        public web.data.entities.article_category GetArticleCategoryByID(int category_id)
        {
            using (var data = new baseEntities())
            {
                web.data.entities.article_category art = data.tbl_article_category.Where(p => p.category_id == category_id).SingleOrDefault();
                return art;
            }
        }

        public void editArticleCategory(article_category cate, List<article_category_item> items)
        {
            using (var data = new baseEntities())
            {
                var edit_cate = data.tbl_article_category.Where(p => p.category_id == cate.category_id).SingleOrDefault();

                if (edit_cate != null)
                {
                    edit_cate.category_status = cate.category_status;
                    edit_cate.category_sort = cate.category_sort;
                    edit_cate.category_parent = cate.category_parent;
                    edit_cate.category_param = cate.category_param;
                    edit_cate.images = cate.images;
                    data.SaveChanges();
                }

                if (items.Count > 0)
                {
                    foreach (var it in items)
                    {
                        var item = data.tbl_article_category_item.Where(p => p.category_item_id == it.category_item_id).SingleOrDefault();

                        if (item != null)
                        {
                            item.category_name = it.category_name;
                            item.category_alias = it.category_alias;
                            item.category_desc = it.category_desc;
                            item.category_desc_short = it.category_desc_short;
                            item.category_id = it.category_id;
                            item.category_item_enable = it.category_item_enable;
                            item.category_meta_description = it.category_meta_description;
                            item.category_meta_keyword = it.category_meta_keyword;
                            item.category_title = it.category_title;
                            item.language_id = it.language_id;
                            data.SaveChanges();
                        }
                        else
                        {
                            it.category_id = cate.category_id;
                            data.AddTotbl_article_category_item(it);
                            data.SaveChanges();
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Đệ quy list ID danh mục bai viet adm page (with languageID)
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public StringBuilder GetListCategoryArticleIdBuilder(ref StringBuilder sb, int cateId, int? languageId)
        {
            using (var data = new baseEntities())
            {
                var cate = (from p in data.view_category_article
                            where p.category_parent == cateId && p.language_id == languageId
                            select p.category_id).ToList();
                foreach (var item in cate)
                {
                    sb.Append(item + ",");
                    if (cate.Count() > 0)
                    {
                        GetListCategoryArticleIdBuilder(ref sb, item, languageId);
                    }
                }

                return sb;
            }
        }

        #endregion

        #region article

        public void updateStatusArticle(article art)
        {
            using (var data = new baseEntities())
            {
                var art_change_status = data.tbl_article.Where(p => p.article_id == art.article_id).SingleOrDefault();

                art_change_status.article_status = art.article_status;
                data.SaveChanges();
            }
        }

        public int createArticle(article art, List<article_item> items, List<article_ref_category> list_ref)
        {
            using (var data = new baseEntities())
            {
                foreach (var item in items)
                {
                    art.tbl_article_item.Add(item);
                }
                data.AddTotbl_article(art);
                foreach (var it in list_ref)
                {
                    art.tbl_article_ref_category.Add(it);
                }
                data.SaveChanges();

                return art.article_id;
            }
        }

        /// <summary>
        /// get list Article Items for article_id
        /// </summary>
        /// <param name="category_id"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public List<web.data.entities.article_item> GetListArticleItemsByArticleID(int article_id, int? languageId)
        {
            using (var data = new baseEntities())
            {
                List<web.data.entities.article_item> list = new List<article_item>();
                if (languageId != null)
                {
                    list = data.tbl_article_item.Where(p => p.article_id == article_id && p.language_id == languageId).ToList();
                }
                else
                {
                    list = data.tbl_article_item.Where(p => p.article_id == article_id).ToList();
                }

                return list;
            }
        }

        public web.data.entities.article GetArticleByID(int article_id)
        {
            using (var data = new baseEntities())
            {
                web.data.entities.article art = data.tbl_article.Where(p => p.article_id == article_id).SingleOrDefault();
                return art;
            }
        }

        public List<int> GetListCategoryByArticleID(int article_id)
        {
            using (var data = new baseEntities())
            {
                var list = data.tbl_article_ref_category.Where(p => p.article_id == article_id).Select(p => p.category_id ?? 0).ToList();
                return list;
            }
        }

        public void DeleteAllArticleRefByArticleID(int article_id)
        {
            using (var data = new baseEntities())
            {
                var list = data.tbl_article_ref_category.Where(p => p.article_id == article_id).ToList();
                foreach (var it in list)
                {
                    data.tbl_article_ref_category.DeleteObject(it);
                    data.SaveChanges();
                }
            }
        }

        public void editArticle(article art, List<article_item> items, List<article_ref_category> list_ref)
        {
            using (var data = new baseEntities())
            {
                var edit_art = data.tbl_article.Where(p => p.article_id == art.article_id).SingleOrDefault();

                if (edit_art != null)
                {
                    edit_art.article_status = art.article_status;
                    edit_art.images = art.images;
                    edit_art.modifyed = art.modifyed;
                    edit_art.modifyed_by = art.modifyed_by;
                    edit_art.url = art.url;
                    data.SaveChanges();
                }

                if (items.Count > 0)
                {
                    foreach (var it in items)
                    {
                        var item = data.tbl_article_item.Where(p => p.article_item_id == it.article_item_id).SingleOrDefault();

                        if (item != null)
                        {
                            item.title = it.title;
                            item.alias = it.alias;
                            item.article_item_enable = it.article_item_enable;
                            item.full_text = it.full_text;
                            item.intro_text = it.intro_text;
                            item.language_id = it.language_id;
                            item.meta_desc = it.meta_desc;
                            item.meta_key = it.meta_key;
                            item.related = it.related;
                            item.tag = it.tag;
                            data.SaveChanges();
                        }
                        else
                        {
                            it.article_id = art.article_id;
                            data.AddTotbl_article_item(it);
                            data.SaveChanges();
                        }
                    }
                }

                foreach (var it in list_ref)
                {
                    edit_art.tbl_article_ref_category.Add(it);
                }
                data.SaveChanges();

            }
        }

        public void deleteArticle(int article_id)
        {
            using (var data = new baseEntities())
            {
                var em = data.tbl_article.Where(p => p.article_id == article_id).FirstOrDefault();

                if (em != null)
                {
                    //Xoa article_item of category
                    var list_item = data.tbl_article_item.Where(p => p.article_id == em.article_id).ToList();
                    foreach (var it in list_item)
                    {
                        data.DeleteObject(it);
                    }

                    //Xoa article_category in ref article
                    var list_ref = data.tbl_article_ref_category.Where(p => p.article_id == em.article_id).ToList();
                    foreach (var it in list_ref)
                    {
                        data.DeleteObject(it);
                    }

                    //Xoa employee object
                    data.DeleteObject(em);
                    data.SaveChanges();
                }
            }
        }

        //Xoa article_item of category
        public void deleteArticleItem(int article_id, int languageid)
        {
            using (var data = new baseEntities())
            {
                var it = data.tbl_article_item.Where(p => p.article_id == article_id && p.language_id == languageid).FirstOrDefault();
                if (it != null)
                {
                    data.DeleteObject(it);
                    data.SaveChanges();
                }
            }
        }
        #endregion

        #region v_article

        /// <summary>
        /// Get list article for adm 
        /// </summary>
        /// <param name="cateId"></param>
        /// <param name="orderBy"></param>
        /// <param name="orderType"></param>
        /// <returns></returns>
        public PagedList<v_article> GetListArticleByCategoryID(int? languageId, int? cateId, int pageindex, int pagesite, string orderBy, string orderType, out int total)
        {
            using (var data = new baseEntities())
            {
                IQueryable<v_article> list;

                if (cateId != null && cateId != 0)
                {
                    StringBuilder sb = new StringBuilder();
                    sb = GetListCategoryArticleIdBuilder(ref sb, (int)cateId);
                    if (sb.ToString().EndsWith(","))
                    {
                        var arrId = sb.ToString().Substring(0, sb.Length - 1).Split(',');
                        int[] arrIdInt = arrId.Select(n => Convert.ToInt32(n)).ToArray();
                        list = (from p in data.view_article
                                where p.category_id == cateId && (p.category_id == cateId || arrIdInt.Contains(p.category_id ?? 0))
                                select p).DistinctBy(p => p.article_id).AsQueryable<v_article>();
                    }
                    else
                    {
                        list = (from p in data.view_article
                                where p.category_id == cateId
                                select p).DistinctBy(p => p.article_id).AsQueryable<v_article>();
                    }

                }
                else
                {
                    list = (from p in data.view_article
                            select p);
                }

                if (languageId != null)
                {
                    list = list.Where(p => p.language_id == languageId);
                }

                if (!string.IsNullOrEmpty(orderBy) && !string.IsNullOrEmpty(orderType))
                {
                    Type sortByPropType = typeof(v_article).GetProperty(orderBy).PropertyType;

                    //calling the extension method using reflection
                    list = typeof(my_extensions).GetMethod("CustomSort").MakeGenericMethod(new Type[] { typeof(v_article), sortByPropType })
                                                .Invoke(list, new object[] { list, orderBy, orderType }) as IQueryable<v_article>;
                }
                else
                {
                    //if  orderBy null set default is created date
                    list = list.OrderByDescending(p => p.created);
                }

                total = list.Count();

                return list.ToPagedList(pageindex, pagesite);
            }
        }

        public v_article GetArticleViewByID(int article_id, int? languageId)
        {
            using (var data = new baseEntities())
            {
                var art = data.view_article.Where(p => p.article_id == article_id && p.language_id == languageId).FirstOrDefault();
                return art;
            }
        }

        //lay tin cung danh muc
        public List<v_article> ListOtherInCategory(int? languageId, int? cateId, int articleId, int number)
        {
            using (var data = new baseEntities())
            {
                var list = (from p in data.view_article
                            where p.article_id != articleId && p.category_id == cateId
                            orderby p.article_id descending
                            select p).Take(number);
                return list.ToList();
            }
        }

        //lay danh sach nhung Tin lien quan 
        public List<v_article> GetListArticleRelated(int articleId, int numberPost)
        {
            using (var data = new baseEntities())
            {
                var list_left = (from a in data.view_article
                                 join b in data.tbl_article_relate on a.article_id equals b.article_id_b
                                 where b.article_id_a == articleId
                                 select a);
                var list_right = (from d in data.view_article
                                  join c in data.tbl_article_relate on d.article_id equals c.article_id_a
                                  where c.article_id_b == articleId
                                  select d);

                var list_relate = list_left.ToList().Union(list_right);

                if (numberPost == -1)
                {
                    return list_relate.ToList(); //get all
                }
                else
                {
                    return list_relate.Take(numberPost).ToList();
                }
            }
        }
        //lay danh sach nhung tin ko lien quan 
        public PagedList<v_article> GetListArticleNotRelate(int articleId, string text_search, int? languageId, int? cateId, int pageindex, int pagesite)
        {
            using (var data = new baseEntities())
            {
                var list_relate = GetListArticleRelated(articleId, -1).Select(p => p.article_id).ToList();

                // tim bai viet
                IQueryable<v_article> list;
                if (cateId != null && cateId != 0)
                {
                    StringBuilder sb = new StringBuilder();
                    sb = GetListCategoryArticleIdBuilder(ref sb, (int)cateId);
                    if (sb.ToString().EndsWith(","))
                    {
                        var arrId = sb.ToString().Substring(0, sb.Length - 1).Split(',');
                        int[] arrIdInt = arrId.Select(n => Convert.ToInt32(n)).ToArray();
                        list = (from p in data.view_article
                                where p.category_id == cateId && (p.category_id == cateId || arrIdInt.Contains(p.category_id ?? 0))
                                select p).DistinctBy(p => p.article_id).AsQueryable<v_article>();
                    }
                    else
                    {
                        list = (from p in data.view_article
                                where p.category_id == cateId
                                select p).DistinctBy(p => p.article_id).AsQueryable<v_article>();
                    }
                }
                else
                {
                    list = (from p in data.view_article
                            select p).DistinctBy(p => p.article_id).AsQueryable<v_article>();
                }

                if (languageId != null)
                {
                    list = list.Where(p => p.language_id == languageId);
                }

                if (!string.IsNullOrEmpty(text_search))
                {
                    list = list.Where(p => (p.title != null && p.title.Contains(text_search)) || (p.intro_text != null && p.intro_text.Contains(text_search)));
                }
                // tim bai viet

                if (list_relate.Count > 0)
                {
                    list = list.Where(p => list_relate.Contains(p.article_id) == false);
                }
                list = list.Where(p => p.article_id != articleId);

                return list.ToPagedList(pageindex, pagesite);
            }
        }
        #endregion

        #region article relate
        public bool article_relate_add(article_relate item)
        {
            try
            {
                using (var data = new baseEntities())
                {
                    var em = data.tbl_article_relate.Where(p =>
                                    (p.article_id_a == item.article_id_a && p.article_id_b == item.article_id_b) ||
                                    (p.article_id_a == item.article_id_b && p.article_id_b == item.article_id_a)
                                    );

                    if (em == null || em.Count() <= 0)
                    {
                        data.AddTotbl_article_relate(item);
                        data.SaveChanges();

                        return true;
                    }
                    return false;
                }
            }
            catch //(Exception ex)
            {
                return false;
            }
        }


        public bool article_relate_delete(int pid_a, int pid_b)
        {
            try
            {
                using (var data = new baseEntities())
                {
                    var em = data.tbl_article_relate.Where(p =>
                                (p.article_id_a == pid_a && p.article_id_b == pid_b) ||
                                (p.article_id_a == pid_b && p.article_id_b == pid_a)
                                );

                    if (em != null)
                    {
                        foreach (var it in em)
                        {
                            data.DeleteObject(it);
                        }
                        data.SaveChanges();
                    }
                    return true;
                }
            }
            catch //(Exception ex)
            {
                return false;
            }
        }
        #endregion

        /// <summary>
        /// Get list article to sitemap
        /// </summary>
        /// <param name="cateId"></param>
        /// <param name="orderBy"></param>
        /// <param name="orderType"></param>
        /// <returns></returns>
        public List<v_article> GetListArticleByCategoryIdSiteMap(int? languageId, int? cateId)
        {
            using (var data = new baseEntities())
            {
                IQueryable<v_article> list;
                languageId = languageId ?? web.component.global_const.LANGUAGE_ID_DEFAULT;   //default language 1 (vi) if null language

                if (cateId != null && cateId != 0)
                {

                    list = (from p in data.view_article
                            join prc in data.tbl_article_ref_category on p.article_id equals prc.article_id
                            where prc.category_id == cateId && p.language_id == languageId && p.article_status == 1
                            select p).DistinctBy(p => p.article_id).AsQueryable<v_article>();
                }
                else
                {
                    list = (from p in data.view_article
                            where p.language_id == languageId
                            select p).DistinctBy(p => p.article_id).AsQueryable<v_article>();
                }



                return list.ToList();
            }
        }
    }
}